package com.yantong.controller.mall;

import com.yantong.annotation.Log;
import com.yantong.controller.BaseController;
import com.yantong.result.ResultVo;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.Notice;
import com.yantong.service.NoticeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 公告控制类
 * @author kanghaixin
 * @date 2018/8/29.20:05
 */
@Controller
@RequestMapping("/mall/notice")
public class NoticeController extends BaseController {

    private static final String prefix = "mall/notice";
    @Autowired
    private NoticeService noticeService;


    /**
     * 公告列表页面
     * @return
     */
    @GetMapping()
    @RequiresPermissions("mall:notice:notice")
    public String notices(Model model) {
        model.addAllAttributes(getCentreIdModelInfo());
        return prefix + "/notices";
    }

    /**
     * 公告列表数据
     * @param title
     * @param centreId
     * @param offset
     * @param limit
     * @return
     */
    @ResponseBody
    @RequiresPermissions("mall:notice:notice")
    @GetMapping("/list")
    public PageUtils<Notice> listNotices(String title,Long centreId, int offset, int limit) {
        centreId = centreId == null?getUserCentreId():centreId;
        return noticeService.listNotices(title, centreId, offset, limit);
    }

    /**
     * 公告发布页面
     * @return
     */
    @RequiresPermissions("mall:notice:add")
    @GetMapping("/add")
    public String add(Model model) {
        model.addAllAttributes(getCentreIdModelInfo());
        return prefix + "/add";
    }


    /**
     * 公告发布
     * @param notice
     * @return
     */
    @Log(module = "公告管理", operation = "新增公告")
    @ResponseBody
    @RequiresPermissions("mall:notice:add")
    @PostMapping("/save")
    public ResultVo save(Notice notice) {
        if (notice == null) {
            return ResultVo.error();
        }
        notice.setAid(getUserId());
        notice.setAname(getNikeName());

        if (noticeService.saveNotice(notice) <1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 不显示操作
     *
     * @param noticeId
     * @return
     */
    @RequiresPermissions("mall:notice:showControl")
    @ResponseBody
    @PostMapping("/disableShow")
    public ResultVo disableShow(Long noticeId) {
        if (noticeId == null) {
            return ResultVo.error();
        }
        if (noticeService.disableShow(noticeId, getUserId(), getNikeName()) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 显示操作
     *
     * @param noticeId
     * @return
     */
    @RequiresPermissions("mall:notice:showControl")
    @ResponseBody
    @PostMapping("/enableShow")
    public ResultVo enableShow(Long noticeId) {
        if (noticeId == null) {
            return ResultVo.error();
        }
        if (noticeService.enableShow(noticeId, getUserId(), getNikeName()) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 公告编辑页面
     * @param noticeId
     * @param model
     * @return
     */
    @RequiresPermissions("mall:notice:edit")
    @GetMapping("/edit/{noticeId}")
    public String edit(@PathVariable Long noticeId, Model model) {
        model.addAttribute("notice", noticeService.getByKey(noticeId));
        model.addAllAttributes(getCentreIdModelInfo());
        return prefix +"/edit";
    }

    /**
     * 公告更新
     * @param notice
     * @return
     */
    @Log(module = "公告管理", operation = "编辑公告")
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions("mall:notice:edit")
    public ResultVo update(Notice notice) {
        if (notice == null || notice.getId() == null) {
            return ResultVo.error();
        }
        notice.setAid(getUserId());
        notice.setAname(getNikeName());
        if (noticeService.updateNotice(notice)< 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 公告逻辑删除
     * @param noticeId
     * @return
     */
    @Log(module = "公告管理", operation = "删除公告")
    @ResponseBody
    @RequiresPermissions("mall:notice:remove")
    @PostMapping("/remove")
    public ResultVo remove(Long noticeId) {
        if (noticeId == null) {
            return ResultVo.error();
        }
        Notice notice = new Notice();
        notice.setId(noticeId);
        notice.setAid(getUserId());
        notice.setAname(getNikeName());
        notice.setDel(1);
        if (noticeService.updateNotice(notice)< 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Log(module = "公告管理", operation = "批量删除公告")
    @ResponseBody
    @PostMapping("/batchRemove")
    @RequiresPermissions("mall:notice:batchRemove")
    public ResultVo batchRemove(@RequestParam("ids[]") Long[] ids) {
        if (ids.length < 1) {
            return ResultVo.error();
        }
        if (noticeService.batchRemove(Arrays.asList(ids))< 1){
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

}
